package Card.game;

import java.util.ArrayList;
import java.util.Collections;

/*
 * Deal with the deck of card and random the card and give it to player.
 * @author Juthamas Utamaphethai 5510546964
 * @author Natcha Chidbhob 5510546026
 * 
 */
public class Card {
	/** all amount of bet from client. */
	private int stock = 0;
	/** store all the cards. */
	private ArrayList<Integer> list = new ArrayList<Integer>();
	/** store the random cards. */
	private ArrayList<Integer> random = new ArrayList<Integer>();

	/*
	 * Initialize the cards in each round.
	 */
	public Card() {
		list.clear();
		// add number of card
		for (int i = 0; i < 52; i++) {
			list.add(i + 1);
		}
		// shuffle card
		Collections.shuffle(list);
	}

	/**
	 * To get the random number.
	 * 
	 * */
	public void getRandom() {
		int num = list.get(0);
		random.add(num);
		list.remove(0);
	}

	/**
	 * Find the max value of cards that each client receive.
	 * 
	 * @return max value
	 * */
	public int findMax() {
		int max = Collections.max(random);
		return max;
	}

	/**
	 * tell all random number
	 * 
	 * @return nnew array of random number.
	 * */
	public String[] getAllRandom() {
		String[] nnew = new String[random.size() + 1];
		for (int i = 0; i <= random.size(); i++) {
			if (i == 0)
				nnew[i] = "card";
			else
				nnew[i] = random.get(i - 1) + "";
		}
		return nnew;
	}// for showing picture in clientUI

	/*
	 * To withdraw the bet money from client who join this round.
	 * 
	 * @param init money of client.
	 * 
	 * @param bet the bet money.
	 * 
	 * @return remain money.
	 */
	public int getNewMon(int init, int bet) {
		return init - bet;
	}

	/*
	 * Find that who win in each round.
	 * 
	 * @return the order of player.
	 */
	public int findWinner() {
		int index = random.indexOf(findMax());
		clear();
		System.out.println(random);
		return index + 1; // the player order(tell that p[index+1] is winner)
	}

	/** clear the array of random cards. */
	public void clear() {
		for (int i = 0; i < random.size(); i++) {
			random.clear();
		}

	}

	/*
	 * Tell the number of remain card.
	 * 
	 * @return number of remaining.
	 */
	public int getRemainCard() {
		return list.size();
	}

	/**
	 * Random the card for all client.
	 * 
	 * @param cli
	 *            the number of client.
	 * */
	public void makeAllRandom(int cli) {
		for (int i = 0; i < cli; i++)
			getRandom();
	}

	/** sum the bet money from each user to stock */
	public void sumBet(int bet) {
		stock += bet;
		System.out.println("stock" + stock);
	}

	/*
	 * clear the summation of bet in that round.
	 */
	public void setSumBet() {
		stock = 0;
	}

	/*
	 * Tell the summation of bet from all players in each round.
	 * 
	 * @return stock.
	 */
	public int getAllBet() {
		return stock;
	}

}
